import React from 'react'
import Admin from "../../../layout/Admin";
import TopicModel from "../../../model/topic";
import {defaultEnd, defaultStart} from "../../user/user-total/user-total";
import Info from "../../../service/info";
import {Checkbox, DatePicker, Table, Popover, Input, Button, Popconfirm} from "antd";
import PaginationFooter from "../../../component/PaginationFooter/PaginationFooter";
import moment from "moment";
const log = console.log.bind(console)

class TopicComments extends React.Component {
    constructor(props) {
        super(props)
        this.model = new TopicModel()
        this.state = {
            list: [],
            p: 0,
            start: defaultStart,
            end: defaultEnd,
            timeCheck: false,
            txt: "",
        }
        this.columns = [
            // Comnum: 0
            // Hfavatar: "https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83epxwHdJVlc8icE6uGFHR6uXrSnOfj1nWkm71sgKoMzXW2zYet9K1STCTiafwiaF4zzRAfueJVamcvt9w/132"
            // Hfeduid: "6651b5fa-1ae5-44a8-b37d-717dc6064357"
            // Hfid: -1

            // Hfname: "ps"
            // Hftime: 1615858792
            // Hftxt: "龙"
            // Hfuid: "273fdb50-3a3a-41b1-bfb9-9b2ce26722ca"
            // Hfvip: 0
            // Htid: 530
            // Id: 1767
            // Lzcomnum: 0
            // Zan: 0
            // Zannum: 0
            // Zant: 1615858792
            // Id        int64    //唯一编码
            {
                dataIndex: 'Id',
                title: 'Id',
            },
            // Htid      int64    //这条评论的话题id
            {
                dataIndex: 'Htid',
                title: '话题Id',
            },
            // Hfuid     string   //评论的uid
            {
                dataIndex: 'Hfuid',
                title: '评论人uid',
                render: uid => <Popover content={<span>{uid}</span>}
                                        title="Uid"
                                        trigger="click"
                >
                    <a>Uid</a>
                </Popover>
            },
            // Hfavatar  string   //评论者的头像
            {
                dataIndex: 'Hfavatar',
                title: '评论人头像',
                render: (src) => <img src={src} alt='' style={{width: 50,}}/>
            },
            // Hfname    string   //评论的昵称
            {
                dataIndex: 'Hfname',
                title: '评论人昵称',
            },
            // Hflv int      //评论的等级
            {
                dataIndex: 'Hflv',
                title: '评论人等级',
            },
            // Hfvip     int      //评论者的vip
            {
                dataIndex: 'Hfvip',
                title: '评论人VIP',
            },
            // Hftxt     string   //评论内容
            {
                dataIndex: 'Hftxt',
                title: '评论内容',
                render: txt => <Popover content={<span>{txt}</span>}
                                        title="评论内容"
                                        trigger="click"
                >
                    <a>评论内容</a>
                </Popover>
            },
            // Hftime     string   //评论时间
            {
                dataIndex: 'Hftime',
                title: '评论时间',
                render: time => moment(new Date(time * 1000)).format('YYYY-DD-MM HH:mm')
            },
            // Hfeduid   string   //被评论的uid
            {
                dataIndex: 'Hfeduid',
                title: '被评论的uid',
                render: uid => <Popover content={<span>{uid}</span>}
                                        title="Uid"
                                        trigger="click"
                >
                    <a>Uid</a>
                </Popover>
            },

            // Lzcomnum  int      //关于楼主的评论条数
            // Comnum    int      //被评论的次数
            {
                dataIndex: 'Comnum',
                title: '本条被评论次数',
            },
            {
                dataIndex: 'Lzcomnum',
                title: '楼主回复次数',
            },
            {
                title: '操作',
                render: (record) => {
                    return <div className='action-links'>
                        <Popconfirm title='确认删除？'
                                    onConfirm={() => this.delete(record)}
                        >
                            <a>删除</a>
                        </Popconfirm>

                    </div>
                }
            }
        ]
    }

    componentDidMount() {
        this.all()
    }

    all() {
        const params = {
            P: this.state.p,
        }

        if (this.state.timeCheck) {
            // 2006-01-02 15:04:05
            const start = parseInt(this.state.start.format('X'))
            const end = parseInt(this.state.end.format('X'))
            params.Starttime = start
            params.Endtime = end
        }
        if (this.state.txt.length > 0) {
            params.Txt = this.state.txt
        }
        this.model
            .comments(params)
            .then(res => {
                const data = res.data.ok
                log(data)
                this.setState({
                    list: data,
                })
            })
    }

    delete = (record) => {
        const params = {
            CommentId: record.Id,
        }
        this.model
            .deleteComment(params)
            .then(res => {
                if (res.data.ok) {
                    Info.success('删除成功')
                }
                this.all()
            })
    }

    onDateChange = (value,) => {
        const [start, end,] = value
        this.setState({
            start,
            end,
        }, () => {
            // this.get()
            this.all()
        })
    }

    search = () => {
        this.setState({
            p: 0,
        }, () => this.all())
    }

    prevPage = () => {
        this.setState({
            p: this.state.p - 1,
        }, () => this.all())
    }

    nextPage = () => {
        this.setState({
            p: this.state.p + 1,
        }, () => this.all())
    }

    render() {
        return <Admin>
            <div className='list-container'>
                <div
                    style={{marginBottom: '8px',}}
                >
                    <Checkbox
                        checked={this.state.timeCheck}
                        onChange={() => {
                            this.setState({
                                timeCheck: !this.state.timeCheck,
                            }, () => {
                                this.all()
                            })
                        }}
                    >
                        时间:
                    </Checkbox>
                    <DatePicker.RangePicker
                        showTime={{ format: 'HH:mm:ss' }}
                        format="YYYY-MM-DD HH:mm:ss"
                        onChange={this.onDateChange}
                        defaultValue={[
                            this.state.start,
                            this.state.end,
                        ]}
                        disabled={this.state.timeCheck === false}
                    />
                </div>
                <div
                    style={{marginBottom: '8px',}}
                >
                    <Input style={{width: '50%'}} placeholder='输入评论内容' value={this.state.txt} onChange={(e) => this.setState({txt: e.target.value,})}/>
                    <Button onClick={this.search}>搜索</Button>
                </div>

                <Table
                    dataSource={this.state.list}
                    columns={this.columns}
                    pagination={false}
                    rowKey={(record, index) => index}
                />
                <PaginationFooter
                    pageIndex={this.state.p}
                    prevPage={this.prevPage}
                    nextPage={this.nextPage}
                />
            </div>
        </Admin>
    }

}

export default TopicComments